package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.ViewDLPurAdjPriceToF4QueryCmd;
import com.jackrain.nea.fc.dl.mapper.ViewDLPurAdjPriceToF4Mapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class ViewDLPurAdjPriceToF4QueryCmdImpl extends CommandAdapter implements ViewDLPurAdjPriceToF4QueryCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        ViewDLPurAdjPriceToF4Mapper viewDLPurAdjPriceToF4Mapper = ApplicationContextHandle.getBean(
                ViewDLPurAdjPriceToF4Mapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(
                map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ViewDLPurAdjPriceToF4QueryCmdImpl--param-->{}",param);


        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        Object[] ids = param.getJSONArray("IDS").toArray();
        String action = param.getString("action");
        JSONArray jsonArray = new JSONArray();

        if ("accept".equals(action)) {
            List<HashMap> list = viewDLPurAdjPriceToF4Mapper.selPurAdjPriceItemData(ids);
            for (HashMap hashMap : list) {
                BigDecimal priceCost = TypeUtils.castToBigDecimal(hashMap.get("PRICE_COST"));
                BigDecimal dpriceList = TypeUtils.castToBigDecimal(hashMap.get("DPRICE_LIST"));
                if (null != priceCost && null != dpriceList) {
                    int r = dpriceList.compareTo(BigDecimal.ZERO);
                    if (r != 0) {
                        BigDecimal disCount = priceCost.divide(dpriceList, 2, BigDecimal.ROUND_HALF_UP);
                        //折扣
                        hashMap.put("DISCOUNT", disCount);
                    }
                }

                JSONObject js = new JSONObject(hashMap);
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        }

        vh.put("data", jsonArray);
        return vh;
    }


}
